﻿/*
* 时间：2019年7月 
* 作者：潘意
*/
using EMRView.Extensions.HCAttribute;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Windows.Forms;

namespace EMRView.Extensions.Command
{
    public partial class CommandEditor : Form
    {

        public BaseCommand SelectedCommand { get; set; }

        private readonly string _value = "";

        public CommandEditor(string value)
        {
            _value = value;
            InitializeComponent();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
            SelectCommad();
        }

        private void lv_Command_DoubleClick(object sender, EventArgs e)
        {
            SelectCommad();
        }

        private void SelectCommad()
        {
            if (lv_Command.SelectedItems != null && lv_Command.SelectedItems.Count > 0)
            {
                this.SelectedCommand = (lv_Command.SelectedItems[0].Tag as BaseCommand);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void CommandEditor_Load(object sender, EventArgs e)
        {
            try
            {
                var baseCommand = typeof(BaseCommand);
                var list = System.AppDomain.CurrentDomain.GetAssemblies();
                foreach (var assembly in list)
                {
                    List<Type> types = new List<Type>();
                    Type[] typeArray;
                    try
                    {
                        typeArray = assembly.GetTypes();
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                    for (int j = 0; j < typeArray.Length; j++)
                    {
                        var t = typeArray[j];
                        if (t.BaseType != null)
                        {
                            if (baseCommand == t.BaseType)
                            {
                                types.Add(t);
                            }
                        }
                    }

                    bool isHave = false;
                    lv_Command.Groups.Add(new ListViewGroup { Name = "默认", Tag = null, Header = "默认" });
                    foreach (var type in types)
                    {
                        var attributes = type.GetCustomAttributes(false);
                        var commandType = CommandType.默认;
                        foreach (var attr in attributes)
                        {
                            if (attr is CommandAttribute)
                            {
                                var a = (EMRView.Extensions.HCAttribute.CommandAttribute)attr;
                                commandType = a.CommandType;
                                foreach (ListViewGroup g in lv_Command.Groups)
                                {
                                    if (g.Name == commandType.ToString())
                                    {
                                        isHave = true;
                                    }
                                }
                                if (!isHave)
                                {
                                    var group = new ListViewGroup { Name = a.CommandType.ToString(), Tag = a, Header = a.CommandType.ToString() };
                                    lv_Command.Groups.Add(group);
                                }
                                var item = new ListViewItem(new string[] { a.CommandName, a.CommandExplain });
                                if (a.CommandName == _value)
                                {
                                    item.Selected = true;
                                }
                                item.Tag = assembly.CreateInstance(type.FullName);
                                item.Group = lv_Command.Groups[a.CommandType.ToString()];
                                lv_Command.Items.Add(item);
                                isHave = false;
                            }
                        }
                    }
                }
                lv_Command.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
